import { EventTypes as events } from './eventTypes'

export const EventTypes = events

/**
 * 事件管理器
 * @description 【重要】确保事件已经在evetTypes.ts里面配置
 */
export const EventManager = {
  /**
   * 同uni.$once，文档: http://uniapp.dcloud.io/api/window/communication?id=once
   */
  $once (eventName: events, callback: (result: any) => void) {
    uni.$once(eventName, data => callback(data))
  },

  /**
   * 同uni.$on: http://uniapp.dcloud.io/api/window/communication?id=on
   */
  $on (eventName: events, callback: (result: any) => void) {
    uni.$on(eventName, data => callback(data))
  },

  /**
   * 同uni.$emit: http://uniapp.dcloud.io/api/window/communication?id=emit
   */
  $emit (eventName: events, payload?: object) {
    uni.$emit(eventName, payload)
  },

  /**
   * 同uni.$off: http://uniapp.dcloud.io/api/window/communication?id=off
   */
  $off (eventNames?: events | Array<events>, callback?: ((result: any) => void) | undefined) {
    uni.$off(eventNames, callback)
  },
}
